package com.example.newdemo.adapter;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.newdemo.R;
import com.example.newdemo.bean.ImageBean;
import com.example.newdemo.bean.IndexBean;
import com.example.newdemo.utils.LogUtils;

import java.util.List;

public class BigImageAdapter extends RecyclerView.Adapter<BigImageAdapter.BigImagViewHolder> {


    private Context context;
    private List<ImageBean> indexList;
    private OnItemStateIndexListener listener;

    public BigImageAdapter(Context context , List<ImageBean> indexList){
        this.context = context;
        this.indexList = indexList;
    }

    public void setOnItemStateListener(OnItemStateIndexListener listener) {
        this.listener = listener;
    }

    public void setIndexList(List<ImageBean> indexList){
        this.indexList = indexList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BigImagViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_big_image, parent, false);

//        View view = new TextView(context);
        BigImagViewHolder holder = new BigImagViewHolder(view);

        LogUtils.d("  ***  onCreateViewHolder  viewType = " + viewType + " ;  " + holder);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BigImagViewHolder holder, int position) {

        LogUtils.d("  ***  onBindViewHolder  postion = " + position + " ;  " + holder);

        ImageBean bean = indexList.get(position);
//        LogUtils.d("  ====  " + bean.imageId);

        holder.rlBody.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                listener.onItemClick(view,position);
            }
        });

//        holder.imageView.setImageDrawable(context.getResources().getDrawable(bean.imageId));
        holder.imageView.setImageResource(bean.imageId);
        holder.textView.setText("postion = " + position);

//        BitmapDrawable bitmapDrawable = new BitmapDrawable();
//        holder.imageView.setImageBitmap();

    }

    @Override
    public int getItemCount() {
        return indexList.size();
    }


    public  class BigImagViewHolder extends RecyclerView.ViewHolder{

        public ImageView imageView;
        public TextView textView;
        public RelativeLayout rlBody;
        public BigImagViewHolder(View v) {
            super(v);
            imageView = v.findViewById(R.id.image_big_image_body);
            rlBody = v.findViewById(R.id.ll_item_big_image_body);
            textView = v.findViewById(R.id.tv_image_index);
        }


    }


    public interface OnItemStateIndexListener {
        void onItemClick(View view, int position);
    }


    // 当viewHolder ， 即将被回收 放入到pool中的时候
    @Override
    public void onViewRecycled(@NonNull BigImagViewHolder holder) {
        super.onViewRecycled(holder);
    }



}
